﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;
using System.Collections.Generic;
using Nova_BLL;
using Nova_MOD;

public partial class System_warehs_WarehListAdd : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    string strSQL = "";
    int iCount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = 0;  //0表示立即过期
        Response.Cache.SetNoStore();//禁用缓存
        Response.AppendHeader("Pragma", "no-cache");// 是不要网页存于缓存之中

        LoginLogic.IsLogin();

        if (!IsPostBack)
        {
            Bind_WareHs();
            SetFirstPageLoad();
        }

        //验证TextBox
        string strJs = "";
        strJs = "return Validate(";
        strJs += "'txtvWareHNo','仓库编号不为空','r'";
        strJs += ",'txtvWareHName','仓库名称不为空','r'";
        strJs += ");";
        ibtnOK.Attributes.Add("onclick", strJs);
        ibtnContinue.Attributes.Add("onclick", strJs);
    }

    private void Bind_WareHs()
    {
        string strSQL = "SELECT distinct(WareHsType) FROM TB_WareHs";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ds = dbHelper.ExecuteDataSet(cmd);
        ddlvWareH.DataSource = ds;
        ddlvWareH.DataBind();
    }

    private void SetFirstPageLoad()
    {
       

        if (Request.QueryString["Mode"] == "Add")
        {
            
            panelContinue.Visible = true;
            lblTitle.Text = "系统管理-用户新增";
        }
        else if (Request.QueryString["Mode"] == "Update")
        {
            lblTitle.Text = "系统管理-用户修改";
            panelContinue.Visible = false;
            txtvWareHNo.ReadOnly = true;
            ds = QueryWareHsDataSet(Request.QueryString["WareHsCode"]);//前一个页面传过来的参数WareHsCode
            if (ds.Tables[0].Rows.Count > 0)
            {
                txtvWareHNo.Text = ds.Tables[0].Rows[0]["WareHsCode"].ToString().Trim();
                txtvWareHName.Text = ds.Tables[0].Rows[0]["WareHsName"].ToString().Trim();

                ddlvWareH.SelectedValue = ds.Tables[0].Rows[0]["WareHsType"].ToString().Trim();
                
            }
        }
    }
    /// <summary>
    /// 根据WareHsCode查询仓库表
    /// </summary>
    /// <param name="WareHsCode"></param>
    /// <returns>返回DataSet</returns>
    /// writer:[Kitty]
    /// Create Date:[2010-5-28]
    public DataSet QueryWareHsDataSet(string WareHsCode)
    {
        try
        {
            strSQL = @"SELECT [WareHsCode]
                              ,[WareHsName]
                              ,[WareHsType]
                              FROM [dbo].[TB_WareHs]
                                where [WareHsCode] = @WareHsCode";
            DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
            dbHelper.AddInParameter(cmd, "@WareHsCode", DbType.String, WareHsCode);
            ds = dbHelper.ExecuteDataSet(cmd);

        }
        catch (Exception ex)
        {
            Console.Write(ex.ToString());
        }

        return ds;
    }
 


    ///<summary>
    ///增加仓库表
    ///</summary>
    ///<param name="sQL"></param>
    ///<returns>返回int,0失败,1成功</returnss>
    /// writer:[Kitty]
    /// Create Date:[2010-5-28]
    ///</summary>
    public int AddWareHs(string vWareHsCode, string vWareHsName, string vWareHsType)
    {
        int i = 0;
        try
        {
           
            strSQL = @"INSERT INTO [dbo].[TB_WareHs]
                                       ([WareHsCode]
                                       ,[WareHsName]
                                       ,[WareHsType]
                                       )
                                 VALUES
                                       (@vWareHsCode
                                       ,@vWareHsName
                                       ,@vWareHsType
                                       )";
            DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
            dbHelper.AddInParameter(cmd, "@vWareHsCode", DbType.String, vWareHsCode);
            dbHelper.AddInParameter(cmd, "@vWareHsName", DbType.String, vWareHsName);
            dbHelper.AddInParameter(cmd, "@vWareHsType", DbType.String, vWareHsType);
            

            i = dbHelper.ExecuteNonQuery(cmd);
        }
        catch (Exception ex)
        {
            Console.Write(ex.ToString());
        }
        return i;
    }

    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        
        try
        {
            
            if (Request.QueryString["Mode"] == "Add")
            {

                //如果仓库号存在，提示跳出
                int res = 0;
                res = CheckWareHsNoExists(txtvWareHNo.Text.Trim());
                if (res == 1)
                {
                    ShowBox.MessageBox(this.Page, "用户编号已经存在!");
                    return;
                }
                iCount = AddWareHs(txtvWareHNo.Text.Trim(), txtvWareHName.Text.Trim(), ddlvWareH.SelectedValue.Trim());
            }
            else if (Request.QueryString["Mode"] == "Update")
            {

                iCount = UpdateWareHs(txtvWareHNo.Text.Trim(), txtvWareHName.Text.Trim(), ddlvWareH.SelectedValue.Trim());

            }

            if (iCount > 0) //增加成功或者修改成功
            {
                ShowBox.MessageBox(this.Page, "新增|修改仓库成功!");
                //ibtnOK.Attributes.Add("onclick", "javascript:window.returnValue=123;window.close()");
                this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.opener.document.getElementById('hidden').value='1';window.opener.document.getElementById('form1').submit();top.close();", true);
                return;
            }
           
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    ///<summary>
    ///修改仓库表
    ///</summary>
    ///<param name="sQL"></param>
    ///<returns>返回int,0失败,1成功</returnss>
    /// writer:[Kitty]
    /// Create Date:[2010-5-28]
    ///</summary>
    public int UpdateWareHs(string WareHsCode, string WareHsName, string WareHsType)
    {
        int i = 0;
        try
        {
            strSQL = @"UPDATE  [dbo].[TB_WareHs]
                               SET [WareHsName] = @WareHsName
                                  ,[WareHsType] = @WareHsType
                               WHERE WareHsCode=@WareHsCode";
            DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
            dbHelper.AddInParameter(cmd, "@WareHsCode", DbType.String, WareHsCode);
            dbHelper.AddInParameter(cmd, "@WareHsName", DbType.String, WareHsName);
            dbHelper.AddInParameter(cmd, "@WareHsType", DbType.String, WareHsType);
           
            i = dbHelper.ExecuteNonQuery(cmd);
        }
        catch (Exception ex)
        {
            Console.Write(ex.ToString());
        }
        return i;
    }
    /// <summary>
    /// 检查用户编号是否已经存在
    /// </summary>
    /// <param name="strGroupNo"></param>
    /// <returns>返回int,0不存在;1存在</returns>
    /// writer:[Kitty]
    /// Create Date:[2010-5-28]
    public int CheckWareHsNoExists(string vWareHsCode)
    {
        strSQL = @"select isnull(count(1),0) from TB_WareHs where WareHsCode = @vWareHsCode";
        DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
        dbHelper.AddInParameter(cmd, "@vWareHsCode", DbType.String, vWareHsCode);

        return Convert.ToInt32(dbHelper.ExecuteScalar(cmd));
    }

    protected void ibtnClose_Click(object sender, ImageClickEventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova", "top.close();", true);
    }
    protected void ibtnContinue_Click(object sender, ImageClickEventArgs e)
    {
        int res = 0;
        try
        {
            //如果仓库号存在，提示跳出
            res = CheckWareHsNoExists(txtvWareHNo.Text.Trim());
            if (res == 1)
            {
                ShowBox.MessageBox(this.Page, "用户编号已经存在!");
                return;
            }

            iCount = AddWareHs(txtvWareHNo.Text.Trim(), txtvWareHName.Text.Trim(), ddlvWareH.SelectedValue.Trim());
           
            if (iCount == 1) //添加成功
            {
                txtvWareHNo.Text = "";
                txtvWareHName.Text = "";
                return;
            }
            

           
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}
